package com.party.admin.web.controller.order;

import com.google.common.collect.Maps;
import com.party.admin.biz.file.ExcelExportService;
import com.party.admin.biz.serviceOrder.ServiceOrderBizService;
import com.party.admin.utils.excel.ExportExcel;
import com.party.admin.web.dto.AjaxResult;
import com.party.admin.web.dto.input.common.CommonInput;
import com.party.common.paging.Page;
import com.party.common.utils.LangUtils;
import com.party.common.utils.StringUtils;
import com.party.core.model.charge.PackageMember;
import com.party.core.model.charge.PackageMemberOutput;
import com.party.core.model.charge.PackageMemberWithAuthor;
import com.party.core.model.charge.RemittanceVoucher;
import com.party.core.service.charge.IPackageMemberService;
import com.party.core.service.charge.IRemittanceVoucherService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @program: party
 * @description: 服务订购控制器
 * @author: Hua Qunhao
 * @create: 2019-05-15 09:25
 **/
@Controller
@RequestMapping(value = "serviceOrder")
public class ServiceOrderController {

    protected static Logger logger = LoggerFactory.getLogger(ServiceOrderController.class);
    @Autowired
    private IPackageMemberService packageMemberService;
    @Autowired
    private IRemittanceVoucherService remittanceVoucherService;

    @Autowired
    private ServiceOrderBizService serviceOrderBizService;

    @Autowired
    private ExcelExportService excelExportService;

    /**
     * @Description:服务订购列表
     * @Param: [packageMemberId, type, request]
     * @return: org.springframework.web.servlet.ModelAndView
     * @Author: Hua Qunhao
     * @Date: 2019/5/15
     */
    @RequestMapping(value = "/list")
    public ModelAndView serviceOrderList(PackageMemberWithAuthor packageMember, CommonInput commonInput, Page page) {
        page.setLimit(10);
        ModelAndView modelAndView = new ModelAndView("order/serviceOrderList");
        Map<String, Object> params = CommonInput.appendParams(commonInput);
        List<PackageMemberWithAuthor> packageMembers = packageMemberService.packageMemberWebList(packageMember, params, page);
        modelAndView.addObject("list", packageMembers);
        modelAndView.addObject("page", page);
        modelAndView.addObject("input", commonInput);
        modelAndView.addObject("packageMember", packageMember);
        return modelAndView;
    }

    /**
     * @param packageMemberId
     * @return
     * @Author yifeng
     * @Description 获取汇款凭证
     * @Date 18:17 2019/5/24
     **/
    @ResponseBody
    @RequestMapping(value = "/remittanceInfo")
    public AjaxResult remittanceInfo(String packageMemberId) {
        if (StringUtils.isEmpty(packageMemberId)) {
            return AjaxResult.error("id不能为空");
        }
        try {
            RemittanceVoucher remittanceVoucher = remittanceVoucherService.findByPackageMemberId(packageMemberId);
            PackageMember packageMember = packageMemberService.get(packageMemberId);
            Map<String, Object> map = Maps.newHashMap();
            map.put("accountName", remittanceVoucher.getAccountName());
            map.put("bankAccount", remittanceVoucher.getBankAccount());
            map.put("openingBank", remittanceVoucher.getOpeningBank());
            map.put("price", packageMember.getPrice());
            return AjaxResult.success(map);
        } catch (Exception e) {
            logger.error("获取汇款凭证异常", e);
            return AjaxResult.error("获取汇款凭证异常");
        }
    }

    /**
     * @param id,sign
     * @return
     * @Author hqh
     * @Description 审核
     * @Date 18:17 2019/5/24
     **/
    @ResponseBody
    @RequestMapping(value = "/audit")
    public AjaxResult audit(String id, Integer sign) {
        if (StringUtils.isEmpty(id) || sign == null) {
            return AjaxResult.error("参数异常");
        }
        try {
            PackageMember packageMember = packageMemberService.get(id);
            if (packageMember == null || packageMember.getAuditStatus() != 1 || packageMember.getStatus() != 1) {
                return AjaxResult.error("审核类型异常");
            }
            //通过
            if (1 == sign) {
                serviceOrderBizService.updateSystemRelated(id);
            } else {
                packageMember.setAuditStatus(PackageMember.AUDIT_STATUS_FAILURE);
                packageMemberService.update(packageMember);
            }
            return AjaxResult.success(true);
        } catch (Exception e) {
            logger.error("审核异常", e);
            return AjaxResult.error("审核异常");
        }
    }

    /**
     * 服务订购记录导出
     *
     * @param packageMember
     * @param commonInput
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "exportSystemPayOrder", method = RequestMethod.POST)
    public AjaxResult exportSystemPayOrder(PackageMemberWithAuthor packageMember, CommonInput commonInput, HttpServletResponse response) {
        try {
            String fileName = "服务订购记录.xlsx";
            Map<String, Object> params = CommonInput.appendParams(commonInput);
            List<PackageMemberWithAuthor> packageMembers = packageMemberService.packageMemberWebList(packageMember, params, null);

            List<PackageMemberOutput> packageMemberOutputs = LangUtils.transform(packageMembers, input -> {
                PackageMemberOutput output = PackageMemberOutput.transform(input);
                return output;
            });
            ExportExcel exportExcel = new ExportExcel("", PackageMemberOutput.class).setDataList(packageMemberOutputs);
            String allPath = excelExportService.getExcelPath(fileName, "packageMember", exportExcel);
            return AjaxResult.success((Object) allPath);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error("导出异常");
    }
}
